package com.xindaoapp.frameworkfragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.xindaoapp.framework.MainActivity;
import com.xindaoapp.framework.R;

public class MenuFragment extends Fragment implements OnItemClickListener {

	private View view;
	private List<String> list;
	private ArrayAdapter<String> adapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		ListView listView = (ListView) view.findViewById(R.id.list_view);

		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, android.R.id.text1,
				initData());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
	}

	private List<String> initData() {
		list = new ArrayList<String>();
		list.add("fragment1");
		list.add("fragment2");
		list.add("fragment3");
		list.add("fragment4");
		list.add("fragment5");
		return list;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = LayoutInflater.from(getActivity()).inflate(R.layout.list_view,
				null);
		return view;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Fragment f = null;
		switch (position) {
		case 0:
            f = new HomeFragment();
			break;

		case 1:
		    f = new ClassifyFragment();
			break;
		case 2:
		    f = new CircleFragment();
			break;
		case 3:
		    f = new CartFragment();
			break;
		case 4:
		    f = new CenterFragment(getActivity());
			break;
		}
		switchFragment(f);
	}
    /**
     * 
     1   new MainActivity(); 得到activity的对象
     2   对外暴露一个方法，去改变内容页
     */
	private void switchFragment(Fragment f) {
		// TODO Auto-generated method stub
		if(null != f){
			if(getActivity() instanceof MainActivity){
				((MainActivity)getActivity()).switchFragment(f);
			}
		}
	}

	public void initMenuCateGory(List<String> menuCategoryList) {
		// TODO Auto-generated method stub
		
	}

}
